package com.mr_ladeng.listview;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mr_ladeng.listviewnest.R;

/**
 * 
 * @项目名称：	Mr_ladeng_wish
 * @类名称： 	RedeemTravelActivity.java
 * @创建人：	Mr.ladeng (zbl704@yeah.net)
 * @创建时间： 2015-4-22上午11:56:31
 * @修改备注：    
 * @version 1.0   
 * @类描述：	还愿之旅	列表	父视图
 */
public class RedeemTravelActivity extends Activity implements OnClickListener {
	
	private String TAG = "RedeemTravelActivity";
	
	private List<PullBeanRedeemParent> pullBeanRedeem;
	
	String uid = "1";
	String pagesize = "0";
	private static String flag = "";

	int show = 1 ;
	
	private PraentAdapter adapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_redeem_travel);
		
		init();
		getDataInfo();
		(new UpdateTextTask2(RedeemTravelActivity.this)).execute();
	}

	private void init() {
		
		ListView listView = (ListView) findViewById(R.id.redeem_travel_lv);
		TextView redeem_detail = (TextView) findViewById(R.id.redeem_detail);
		redeem_detail.setOnClickListener(this);
		ImageView redeem_title_back = (ImageView) findViewById(R.id.redeem_title_back);
		redeem_title_back.setOnClickListener(this);
		
		pullBeanRedeem =new ArrayList<PullBeanRedeemParent>();
		adapter = new PraentAdapter(this, pullBeanRedeem);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Toast.makeText(RedeemTravelActivity.this,"第" + arg2 + "个条目", Toast.LENGTH_LONG).show();
		}
	});
	
	
	
	}
	


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.redeem_detail:
//			Intent intent = new Intent(RedeemTravelActivity.this,DetailActivity.class);
//			intent.putExtra("wishid", 1);
//			startActivity(intent);
			break;
		case R.id.redeem_title_back:
			finish();
			break;
		}
	}
	

	/****************	数据请求	start	*********************/
	
	void getDataInfo() {
		
		String wishid = "1";
		
		getRedeem(wishid, uid , pagesize);
		
	}
	
	
	public String getRedeem(String wishid, String uid , String pagesize) {

		(new UpdateTextTask(wishid , uid , pagesize)).execute();

		return "";

	}

	/** 异步任务 请求列表 */
	class UpdateTextTask extends AsyncTask<Void, Integer, Integer> {
		private String wishid;
		private String uid;
		private String pagesize;

		UpdateTextTask(String wishid, String uid , String pagesize) {
			this.wishid = wishid;
			this.uid = uid;
			this.pagesize=pagesize;
		}

		@Override
		protected void onPreExecute() {

		}
		
		@Override
		protected void onPostExecute(Integer result) {
			adapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}



		/**
		 * http://192.168.1.104/realizewish/showtheme/wishid/1/uid/1/pagesize/0/login_uid/1/login_token/94c6eac0f19d37adf0b82e0b982c74f0
		 */
		
		@Override
		protected Integer doInBackground(Void... params) {
			String url= "http://192.168.1.104/realizewish/showtheme/wishid/1/uid/1/pagesize/0/login_uid/1/login_token/94c6eac0f19d37adf0b82e0b982c74f0";

			JSONObject jsonObject = JsonUtils.ReadHttpGet(url);
//			JSONObject jsonObject = JsonUtils.ReadHttpGet(Constant.REDEEM+ "/ wishid/" + wishid+ "/uid/" + uid + "/pagesize/"
//					+ pagesize + "/login_uid/" + App.USER_UUID+"/login_token/"+App.USER_TOKEN);
			

			try {
				if (null != jsonObject)
					flag = jsonObject.getString("flag");

				if ("1".equals(flag)) {

					JSONArray jsonArray = new JSONArray(jsonObject.getString("data"));
					for (int i = 0; i < jsonArray.length(); i++) {

						JSONObject temp = (JSONObject) jsonArray.get(i);

						PullBeanRedeemParent bean = new PullBeanRedeemParent();

						bean.uid = temp.getString("uid");
						bean.headpic = temp.getString("headpic");
						bean.realize_wishid = temp.getString("realize_wishid");

						bean.rea_words_content = temp.getString("rea_words_content");
						bean.praise = temp.getString("praise");

						bean.reply_num = temp.getString("reply_num");
						bean.rea_words_time = temp.getString("rea_words_time");
						bean.comment_yes = temp.getString("comment_yes");
						bean.praise_yes = temp.getString("praise_yes");


						JSONArray jsonArray2 = temp.getJSONArray("picture_address_s");
						Log.i(TAG, jsonArray2.length() + "");
						for (int j = 0; j < jsonArray2.length(); j++) {

							bean.p1 = jsonArray2.optString(0);
							bean.p2 = jsonArray2.optString(1);
							bean.p3 = jsonArray2.optString(2);
							bean.p4 = jsonArray2.optString(3);
							bean.p5 = jsonArray2.optString(4);
							bean.p6 = jsonArray2.optString(5);
							bean.p7 = jsonArray2.optString(6);
							bean.p8 = jsonArray2.optString(7);
							bean.p9 = jsonArray2.optString(8);
						
						}

						pullBeanRedeem.add(bean);
					}

				} else {
					Log.i(TAG, "还愿之旅----请求失败");
				}
			} catch (JSONException e) {
			}
			return null;
		}
	}
	
	/** 异步任务 登录 */
	class UpdateTextTask2 extends AsyncTask<Void, Integer, Integer> {
		private Context context;
		private String flag;
		
		List<PullBeanRedeemChild> pullBeanRedeemChild = new ArrayList<PullBeanRedeemChild>();

		UpdateTextTask2(Context context) {
			this.context = context;
		}

		@Override
		protected void onPreExecute() {

		}
		
		@Override
		protected void onPostExecute(Integer result) {
//			childAdapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}

		@Override
		protected Integer doInBackground(Void... params) {

			JSONObject jsonObject = JsonUtils.ReadHttpGet(Constant.REDEEMREPLY+"/realize_wishid/"+"1"+"/pagesize/"+"5"+"/login_uid/"+"1"+"/login_token/"+"94c6eac0f19d37adf0b82e0b982c74f0");
			if (null != jsonObject) {
				try {
					flag = jsonObject.getString("flag").toString().trim();
					Log.i(TAG, jsonObject.toString());
					if ("1".equals(flag)) {

						JSONArray jsonArray = new JSONArray(jsonObject.getString("data"));
						for (int i = 0; i < jsonArray.length(); i++) {

							JSONObject temp = (JSONObject) jsonArray.get(i);

							PullBeanRedeemChild bean = new PullBeanRedeemChild();
							
							bean.mark = temp.getString("mark");
							bean.uid = temp.getString("uid");
							bean.nickname_1 = temp.getString("nickname_1");
							bean.yid = temp.getString("yid");
							bean.nickname_2 = temp.getString("nickname_2");
							bean.rea_message_content = temp.getString("rea_message_content");
							String string = temp.getString("rea_message_content");//测试数据

							pullBeanRedeemChild.add(bean);
							
						}

					} else {
						Log.i(TAG, "还愿之旅--评论--请求失败");
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

	

		@Override
		protected void onProgressUpdate(Integer... values) {

		}
	}
	
	/****************	数据请求	end	*********************/
	
	

}
